package com.banyuan.servlet.author;

import com.banyuan.dao.AuthorDao;
import com.banyuan.domain.Author;
import com.banyuan.utils.CommonUtils;
import com.banyuan.utils.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/author/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1.先去取前端传送的值
        String userName = req.getParameter("userName");
        String password = req.getParameter("password");
        String repassword = req.getParameter("repassword");
        String nickName = req.getParameter("nickName");
        String imageCode = req.getParameter("imageCode");


        // 2.判断取到的值是否为null或者去掉空格后是否为""
        if (CommonUtils.isEmpty(userName) || CommonUtils.isEmpty(password) || CommonUtils.isEmpty(repassword)
                || CommonUtils.isEmpty(nickName) || CommonUtils.isEmpty(imageCode)) {
            // 给前端传一句话
            req.setAttribute("msg","输入项为空");
            // 跳转到注册页面
            req.getRequestDispatcher("/WEB-INF/pages/author/register.jsp").forward(req,resp);
            return;
            // 阻断下面代码的运行
        }


        // 3.判断验证码是否输入正确----session
        HttpSession session = req.getSession();
        String imagecode = (String)session.getAttribute("IMAGECODE");
         // 忽略大小写去比较是否相等
        if (!imageCode.equalsIgnoreCase(imagecode)) {
            // 给前端传一句话
            req.setAttribute("msg","验证码不正确");
            // 跳转到注册页面
            req.getRequestDispatcher("/WEB-INF/pages/author/register.jsp").forward(req,resp);
            return;
            // 阻断下面代码的运行
        }

        // 4.判断密码和重复密码是否一样
        if (!password.equals(repassword)) {
            // 给前端传一句话
            req.setAttribute("msg","两次密码输入不一样");
            // 跳转到注册页面
            req.getRequestDispatcher("/WEB-INF/pages/author/register.jsp").forward(req,resp);
            return;
            // 阻断下面代码的运行
        }


        // 5.判断用户名是否过长

        if (userName.length() > 30) {
            // 给前端传一句话
            req.setAttribute("msg","名字太长了");
            // 跳转到注册页面
            req.getRequestDispatcher("/WEB-INF/pages/author/register.jsp").forward(req,resp);
            return;
            // 阻断下面代码的运行
        }

        // 6.判断用户名是否和数据库里面的用户名重复
        ResultSet resultSet = null;
        try {
             resultSet = AuthorDao.selectByUserName(userName);
             resultSet.next();
            int countSum = resultSet.getInt("countSum");
            // countSum 是查询出来的统计结果，如果countSum>0，就是名字重复了

            if (countSum > 0) {
                // 给前端传一句话
                req.setAttribute("msg","用户名重复了");
                // 跳转到注册页面
                req.getRequestDispatcher("/WEB-INF/pages/author/register.jsp").forward(req,resp);
                return;
                // 阻断下面代码的运行
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeBy(resultSet);
            // 通过rs去关流
        }


        // 7.如果都没问题的话，将这个用户存储到数据库里
        Author author = new Author();
        author.setUserName(userName);

        // CommonUtils.calculateMD5 将密码加密
        author.setPassword(CommonUtils.calculateMD5(password));
        author.setNickName(nickName);

        try {
            int i = AuthorDao.insert(author);
            if (i > 0) {
                // i>0 就是存储成功了
                req.getRequestDispatcher("/WEB-INF/pages/author/login.jsp").forward(req,resp);
            } else {
                // 给前端传一句话
                req.setAttribute("msg","注册失败，请稍后再试");
                // 跳转到注册页面
                req.getRequestDispatcher("/WEB-INF/pages/author/register.jsp").forward(req,resp);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
